<?php
namespace app\common\model;
use think\model\concern\SoftDelete;
class Follow extends \think\Model{
    protected $resultSetType = 'collection';
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    //关联项目信息
    public function accountInfo()
    {
        return $this->hasOne('Account','id','account_id')->removeOption('soft_delete');
    }

    /**
     * 插入跟进
     * @param string content 跟进内容
     * @param int message_id 用户ID
     * @param string schedule 约访时间
     * @return array 返回信息
     */
    public function inserts($loginInfo)
    {
        $data = input('param.');
        $validate = validate($this -> name);
        if(!$validate->check($data))
            return [
                'message' => $validate->getError()
            ];



        if ($data['schedule']){
            $data['schedule'] = strtotime($data['schedule']);
            if ($data['schedule']<time())
                return [
                    'message' => '约访时间不能小于当前时间'
                ];
            \app\common\model\Message::update([
                'id' => $data['message_id'],
                'schedule' => $data['schedule']
            ]);
        }

        $this -> save([
            'content' => $data['content'],
            'message_id' => $data['message_id'],
            'account_id' => $loginInfo['id'],
            'date' => time()
        ]);

        $info = \app\common\model\Message::where('id',$data['message_id'])->find();
        if ($info['turns_mark'] =='1')
            if ($loginInfo['id'] == $info['account_id'])
                \app\common\model\Message::where('id',$data['message_id'])->update(['turns_mark' =>'0']);

        return [
            'code' => '1',
            'message' => '跟进成功'
        ];
    }
}